//
//  SearchResultViewController.m
//  网易公开课项目
//
//  Created by 林 艺 on 13-11-19.
//  Copyright (c) 2013年 Marklin. All rights reserved.
//

#import "SearchResultViewController.h"
#import "CourseModel.h"
#import "CourseCell.h"
#import "SearchResultCell.h"
#import "UIImageView+WebCache.h"
#import "DetailViewController.h"

@interface SearchResultViewController ()

@end

@implementation SearchResultViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = @"搜索结果";
        self.resultArray = [[NSMutableArray alloc] init];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self _initNavigationBarItem];
    if (self.resultArray.count == 0)
    {
        self.backView.hidden = NO;
        self.tableView.hidden = YES;
    }
    [self.tableView registerNib:[UINib nibWithNibName:@"CourseCell" bundle:nil] forCellReuseIdentifier:@"CourseCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"SearchResultCell" bundle:nil] forCellReuseIdentifier:@"SearchResultCell"];
}

- (void)_initNavigationBarItem
{
    //返回
    UIButton *back = [UIButton buttonWithType:UIButtonTypeCustom];
    [back setBackgroundImage:[UIImage imageNamed:@"btn_left.png"] forState:UIControlStateNormal];
    
    [back setTitle:@"返回" forState:UIControlStateNormal];
    back.titleLabel.font = [UIFont systemFontOfSize:11];
    [back addTarget:self action:@selector(backClicked) forControlEvents:UIControlEventTouchUpInside];
    back.frame = CGRectMake(0, 0, 50, 34);
    UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithCustomView:back];
    self.navigationItem.leftBarButtonItem = leftButton;
}

- (void)backClicked
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    if (section == 0)
    {
        return 1;
    }
    return self.resultArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0)
    {
        SearchResultCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SearchResultCell"];
        cell.resultLabel.text = [NSString stringWithFormat:@"共%d个搜索结果", self.resultArray.count];
        return cell;
    }
    else
    {
        CourseCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CourseCell"];
        CourseModel *model = [self.resultArray objectAtIndex:indexPath.row];
        cell.titleLabel.text = model.title;
        cell.classLabel.text = model.tags;
        cell.playCountLabel.text = [NSString stringWithFormat:@"%@", model.playcount];
        cell.updatePlayCountLabel.text = [NSString stringWithFormat:@"%@", model.updatedPlaycount];
        [cell.courseImageView setImageWithURL:[NSURL URLWithString:model.imagePath]];
        
        return cell;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 1)
    {
        return 92;

    }
    return 44;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 1)
    {
        DetailViewController *detailController = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil];
        
        CourseModel *model = [self.resultArray objectAtIndex:indexPath.row];
        detailController.videoCode = model.plid;
        
        //隐藏tabbarView;
        UIView * view =[self.tabBarController valueForKey:@"tabbarView"];
        view.frame = CGRectMake(-320, view.frame.origin.y, view.frame.size.width, view.frame.size.height);
        
        [self.navigationController pushViewController:detailController animated:YES];
    }
    
}


@end
